//index.js
const Controllers = require('../controllers/Controller.js').controller;
const controllers = require('../controllers/controllerIndex.js').controller;
const app = getApp();
Page({
  data: {
    list: [],
    routers: [
     {
        id: "sy",
        name: '结账收银',
        icon: "/images/home_icon_01.png",
      }, {
        id: "gl",
        name: '会员管理',
        icon: "/images/home_icon_02.png",
      },{
        name: '记账',
        id:"jz",
        icon: "/images/home_icon_03.png",
      },
    ],
    managerGrop:[

      {
        id: "material",
        name: '素材中心',
        icon: "/images/home_icon_04.png",
      }, {
        id: "goodsmanage",
        name: '商品管理',
        icon: "/images/home_icon_05.png",
      },{
        name: '充值会员设置',
        id:"vipSetting",
        icon: "/images/home_icon_06.png",
      },{
        name: '店铺活动',
        id:"shopActive",
        icon: "/images/home_icon_07.png",
      },
      {
        name: '用户管理',
        id:"userManager",
        icon: "/images/home_icon_08.png",
      },
      {
        name: '员工管理',
        id:"personManager",
        icon: "/images/home_icon_09.png",
      },
      {
        name: '资金管理',
        id:"myAccount",
        icon: "/images/home_icon_10.png",
      },
      {
        name: '云商城',
        id:"cloudGoods",
        icon: "/images/home_icon_11.png",
      },
    
    ], 
     storeInfo: { },
  },
  onLoad(option) {
    let that =this
    if(wx.getStorageSync('diantype')==1){
      this.setData({
        list:app.globalData.list
      })
    }else{
      this.setData({
        list:app.globalData.list02
      })
      wx.navigateTo({
        url: '/pages/order/order-manage/order-manage',
      })
    }
    
 
  },

    DateDiff(sDate1, sDate2) {  //sDate1和sDate2是yyyy-MM-dd格式 
      console.log('octo'+sDate1, sDate2)
    var aDate, oDate1, oDate2, iDays; 
    aDate = sDate1.split("-"); 
    oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);  //转换为yyyy-MM-dd格式 
    aDate = sDate2.substring(0,10).split("-"); 
    oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]); 
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数 
    return iDays;  //返回相差天数 
},
  onShow() {
    //获取token
    let token = wx.getStorageSync('token') || '';
    let that =this
    //优先判断token
    if (token) {
      //延时判断微信服务器Session状态
      // setTimeout(() => {
      //   wx.checkSession({
      //     success: () => {
      //       //session_key 未过期，并且在本生命周期一直有效
      //     },
      //     fail: () => {
      //       // session_key 已经失效，需要重新执行登录流程
      //       // this.GetLogin(false);
      //       wx.reLaunch({
      //         url: '/pages/login/index',
      //       })
      //     }
      //   });
      // }, 1500);
    } else {
      //无token，直接登录
      // this.GetLogin();
      wx.reLaunch({
        url: '/pages/login/index',
      })
    }

   //获取店铺首页信息
   controllers.shopInfo({
    sessionId: wx.getStorageSync('token'),
    shopId:wx.getStorageSync('shopId')
  }).then(res =>{
    
    let newTime=new Date().getTime();
    let lastDay=0;
    if(res.data.endTime){
     
    let endTime=new Date(res.data.endTime.replace(/-/g,"/")).getTime();
    lastDay=Math.ceil((endTime-newTime)/1000/24/3600);
    
   
    that.setData({
      lastDay 
    })
    console.log(lastDay)
    wx.setStorageSync('endTime', res.data.endTime)
    }
    that.setData({
      storeInfo : res.data,
     
    })
    
  })
  },
  //跳转续费页面
  toBill(){
    wx.requestSubscribeMessage({
      tmplIds:["fleQPapATpp6thoxCxBw1NSkonsBPrLbeow939kr4bo","Sc3aovgRuyAjgHPCxz2tKwY1udcPno6ZabDj72J5VFo","EK0YhGfRv3PZL5sY9jLOI54XskmKQ3B4aQN1x26rMZQ"],
      success:res=>{
        console.log(res);
      },
      complete:res=>{
        wx.navigateTo({
          url: '/pages/Index/bill/bill',
        })
      }
    })
  },
  tabChange(e){
    let index=e.detail.index;
    console.log(index)
    if(index==0){
      wx.redirectTo({
        url: '/pages/Index/Index'
      })
    }else
    if(index==1){
      wx.redirectTo({
        url: '/pages/order/order-manage/order-manage'
      })
    }else if(index==2){
      wx.redirectTo({
        url: '/pages/my/index'
      })
    }else{
      wx.redirectTo({
        url: '/pages/my/index'
      })
    }
  },
  login() {
    //获取数据
    Controllers.login().then(res => {
      
    })
  },
  
  routerToPage(res){
    console.log(res)
  switch(res.currentTarget.dataset.id){
     case  'sy':
      wx.navigateTo({
        url: "../settlement/CashierCheckout/CashierCheckout"
      })
     break;
     case  'shopActive':
      wx.navigateTo({
        url: "/pages/Index/shopActive/index"
      })
     break;
     case 'jz':
      wx.navigateTo({
        url: "../Accounts/AccountsIndex/AccountsIndex"
      })
     break;
     case 'vipSetting':
      wx.navigateTo({
        url: "../goodsManagement/setVip/setVip"
      })
       break;
     case 'gl':
      wx.navigateTo({
        url: "../userStatistics/Userindex/UserIndex"
      })
     break;
      case 'changeStore':
        wx.navigateTo({
          url: '/pages/login/select_store/index',
        })
        break;
      case 'myAccount':
        wx.navigateTo({
          url: '/pages/my/my_account/index',
        })
        break;  
        case 'userManager':
          wx.navigateTo({
            url: '/pages/userStatistics/Userindex/UserIndex',
          })
          break;
          case 'personManager':
            wx.navigateTo({
              url: '/pages/userStatistics/Personnel/Personnel',
            })
            break;
            case "goodsmanage":
              wx.navigateTo({
                url: '/pages/goodsManagement/goodsManagementIndex/goodsManagementIndex',
              })
              break;
            case "material":
              wx.navigateTo({
                url: '/pages/material/index',
              })
              break;
            case "cloudGoods":
              wx.navigateTo({
                url: '/pages/cloudGoods/index',
              })
            break;
    }
  },
  toDetail(options){
    let id=options.currentTarget.dataset.id;
    if(id==0){
      wx.navigateTo({
        url: '/pages/userStatistics/Userindex/UserIndex',
      })
    }else{
      wx.navigateTo({
        url: '/pages/my/my_account/index',
      })
    }
  }
})